//
//  TranslationPortalView.swift
//  GLUZSwift
//
//  Created by xyanl on 2025/5/8.
//
//  翻译入口
import UIKit
import Localize_Swift
import Reusable

extension TranslationPortalView: NibLoadable {}

enum TranslationPortalType: Int {
    /// 同声传译
    case realtime
    /// 图片翻译
    case picture
    /// 语音翻译
    case audio
    /// 文字翻译
    case word
}


class TranslationPortalView: UIView {

    
    var didSelectedItemBlock: ((TranslationPortalType) -> Void)?
    var closeBlock: (() -> Void)?
    
    @IBOutlet weak var zhMarkView: UIView!
    @IBOutlet weak var ruMarkView: UIView!
    override func awakeFromNib() {
        super.awakeFromNib()
        
        backgroundColor = .clear
        
        zhMarkView.backgroundColor = .clear
        zhMarkView.addRoundedCorners(radius: 12)
        
        ruMarkView.backgroundColor = .white
        ruMarkView.addRoundedCorners(radius: 12)
        
        /// 监听语言的变化
        NotificationCenter.default.addObserver(self, selector: #selector(updateLanguageAction), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
        updateLanguageAction()
    }

    @IBAction func itemAction(_ sender: UIButton) {
        didSelectedItemBlock?(TranslationPortalType(rawValue: sender.tag)!)
    }
    
    @IBAction func closeAction(_ sender: Any) {
        closeBlock?()
    }
    
    // 更新语言
    @objc private func updateLanguageAction() {
        if LanguageManager.isLanguageZH {
            zhMarkView.isHidden = false
            ruMarkView.isHidden = true
            let itemWidth: CGFloat = (xScreenW - 24*2 - 12*2 - 0)*0.5
            let itemHeight = itemWidth*170.0/291.0
            
            var height: CGFloat = 64 + 24
            height += (itemHeight*2 + 10)
            height += 50 // 关闭按钮的高度
            frame = CGRect(x: 0, y: 0, width: xScreenW - 24*2, height: height)
            
        } else {
            zhMarkView.isHidden = true
            ruMarkView.isHidden = false
            let itemWidth: CGFloat = xScreenW - 24*2 - 12*2
            let itemHeight = itemWidth*170.0/589.0
            
            var height: CGFloat = 64 + 24
            height += itemHeight*4
            height += 50 // 关闭按钮的高度
            frame = CGRect(x: 0, y: 0, width: xScreenW - 24*2, height: height)
        }
    }
}
